package org.example.domain.plugin.impl;

import lombok.extern.slf4j.Slf4j;
import org.example.domain.plugin.service.BaseAsyncPlugin;
import org.example.types.model.*;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 模拟插件实现（用于测试）
 */
@Slf4j
// @Component  // 禁用Mock插件，使用真实数据源
public class MockPlugin extends BaseAsyncPlugin {

    @Override
    public String getName() {
        return "mock";
    }

    @Override
    public int getPriority() {
        return 5; // 低优先级
    }

    @Override
    protected Mono<PluginSearchResult> doSearch(String keyword, SearchContext context, Map<String, Object> ext) {
        log.info("Mock plugin searching for keyword: {}", keyword);

        // 模拟网络延迟
        return Mono.delay(java.time.Duration.ofMillis(100))
                .then(Mono.fromCallable(() -> createMockResults(keyword)))
                .map(this::createSuccessResult);
    }

    private List<SearchResult> createMockResults(String keyword) {
        // 创建模拟搜索结果
        Link link1 = Link.builder()
                .type("baidu")
                .url("https://pan.baidu.com/s/mock123")
                .password("mock")
                .build();

        Link link2 = Link.builder()
                .type("aliyun")
                .url("https://www.aliyundrive.com/s/mock456")
                .password("")
                .build();

        SearchResult result1 = SearchResult.builder()
                .uniqueId("mock-1")
                .title(keyword + " - 模拟结果1")
                .content("这是一个模拟的搜索结果，包含关键词: " + keyword)
                .datetime(LocalDateTime.now().minusHours(1))
                .links(List.of(link1))
                .build();

        SearchResult result2 = SearchResult.builder()
                .uniqueId("mock-2")
                .title(keyword + " - 模拟结果2")
                .content("另一个模拟的搜索结果，用于测试: " + keyword)
                .datetime(LocalDateTime.now().minusHours(2))
                .links(List.of(link2))
                .build();

        return List.of(result1, result2);
    }

    @Override
    public String getDescription() {
        return "模拟插件，用于开发和测试";
    }
}